package com.zyuan.lawyerport;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.zyuan.lawyerport.presenter.main.MainPresenterImpl;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import common.base.InitApplication;
import common.base.activity.BaseActivity;

/**
 * description TAB 主框架
 * Created by JustTong on 2018/11/29.
 */
public class MainActivity extends BaseActivity {


    @BindView(R.id.fl_main)
    FrameLayout flMain;
    @BindView(R.id.img_ico1)
    ImageView imgIco1;
    @BindView(R.id.tv_name1)
    TextView tvName1;
    @BindView(R.id.ll_tab1)
    LinearLayout llTab1;
    @BindView(R.id.img_ico2)
    ImageView imgIco2;
    @BindView(R.id.tv_name2)
    TextView tvName2;
    @BindView(R.id.ll_tab2)
    LinearLayout llTab2;
    @BindView(R.id.img_ico3)
    ImageView imgIco3;
    @BindView(R.id.tv_name3)
    TextView tvName3;
    @BindView(R.id.ll_tab3)
    LinearLayout llTab3;
    @BindView(R.id.img_ico4)
    ImageView imgIco4;
    @BindView(R.id.tv_name4)
    TextView tvName4;
    @BindView(R.id.ll_tab4)
    LinearLayout llTab4;
    private MainPresenterImpl mImpl;
    private int index = 0;//默认展示首页
    private ImageView[] tvIco;
    private TextView[] tvName;


    @Override
    public void init() {
        //优化Application启动初始化
        InitApplication.getInstances().init(this);

        tvIco = new ImageView[]{imgIco1, imgIco2, imgIco3, imgIco4};
        tvName = new TextView[]{tvName1, tvName2, tvName3, tvName4};

        //初始化
        mImpl = new MainPresenterImpl(this);

        //初始化导航栏各个tab 关联矢量图标文件 设置图表的大小
        mImpl.initSetIconfont(tvIco);

        //选中的TAB,显示
        mImpl.initTabClick(tvIco, tvName, index, this.getSupportFragmentManager());

        //版本升级检测
        mImpl.initcheckUpdate();

    }

    @Override
    public int setLayout() {
        return R.layout.activity_main_fragment;
    }


    @OnClick({R.id.ll_tab1, R.id.ll_tab2, R.id.ll_tab3, R.id.ll_tab4})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_tab1:
                //Todo  栏目1
                index = 0;
                //选中的TAB,显示
                mImpl.initTabClick(tvIco, tvName, index, this.getSupportFragmentManager());
                break;
            case R.id.ll_tab2:
                //Todo  栏目2
                index = 1;
                //选中的TAB,显示
                mImpl.initTabClick(tvIco, tvName, index, this.getSupportFragmentManager());
                break;
            case R.id.ll_tab3:
                //Todo  栏目3
                index = 2;
                //选中的TAB,显示
                mImpl.initTabClick(tvIco, tvName, index, this.getSupportFragmentManager());
                break;
            case R.id.ll_tab4:
                //Todo  栏目4
                index = 3;
                //选中的TAB,显示
                mImpl.initTabClick(tvIco, tvName, index, this.getSupportFragmentManager());
                break;

            default:
                Logger.e("Error:选中异常，请检查！");
                break;
        }

    }

    @Override
    protected void onPause() {
        JPushInterface.onPause(this);
        super.onPause();
    }

    @Override
    protected void onResume() {
        JPushInterface.onResume(this);
        super.onResume();
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                //退出APP
                mImpl.onExit();
            }
            return true;
        }
        return super.dispatchKeyEvent(event);
    }


    @Override
    public void initLoadStart() {

    }

    @Override
    public void initLoadEnd() {

    }

    @Override
    public void initBindingView(Object mBean) {

    }

}

